package com.juxing.webserver.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.servlet.ModelAndView;

import com.juxing.sdk.util.DigestUtil;
import com.juxing.sdk.util.StringUtil;
import com.juxing.service.userCenter.bean.UserBean;
import com.juxing.webserver.Constant;
import com.juxing.webserver.common.ConfigManager;
import com.juxing.webserver.common.ReturnCodeConstant;

public abstract class BaseAction {
	
	@Autowired
	@Qualifier(value="env")
	private String env;//环境变量
	
	@Autowired
	protected ConfigManager configManager;
	
	private static final Logger logger = LoggerFactory.getLogger(BaseAction.class);

	/**
	 * 返回成功view
	 * @return
	 */
	protected ModelAndView initSuccessView() {
		ModelAndView view = new ModelAndView("base/base");
		view.addObject("code", ReturnCodeConstant.SUCCESS);
		return  view;
	}
	/**
	 * 返回成功view
	 * @param viewPath
	 * @return
	 */
	protected ModelAndView initSuccessView(String viewPath) {
		ModelAndView view = new ModelAndView(viewPath);
		view.addObject("code", ReturnCodeConstant.SUCCESS);
		return  view;
	}
	/**
	 * 返回错误view
	 * @param errorCode
	 * @return
	 */
	protected ModelAndView initErrorView(String errorCode) {
		return  this.initErrorView(errorCode, ReturnCodeConstant.getErrorMsg(errorCode));
	}
	/**
	 * 返回错误view
	 * @param errorCode
	 * @param errMsg
	 * @return
	 */
	protected ModelAndView initErrorView(String errorCode, String errMsg) {
		ModelAndView view = new ModelAndView("base/base");
		view.addObject("code", errorCode);
		view.addObject("errMsg", errMsg);
		
		return  view;
	}
	
	/**
	 * 加密返回view(一般用于加密返回view)
	 * @param request
	 * @param response
	 * @param view
	 * @return
	 */
	protected ModelAndView encryptedView(HttpServletRequest request, HttpServletResponse response, ModelAndView view) {
		return view;
	}
	
	/**
	 * 获取request中的当前用户
	 *   该用户对象是在WebFilter中设置进去的
	 * @param request
	 * @return
	 */
	protected UserBean getUserBean(HttpServletRequest request) {
		return (UserBean)request.getAttribute(Constant.CURR_USER);
	}
	
	protected static final String IMG_CODE_KEY_PREFIX = "JX";
	/**
	 * 校验验证码
	 * @param imgCodeKey
	 * @param imgCode
	 * @return
	 *   -1 验证码key错误
	 *   -2 验证码过期
	 *   -3 验证码错误
	 */
	protected int validateImgCode(String imgCodeKey, String imgCode) {
		try {
			if(StringUtil.isEmpty(imgCodeKey)) {
				return -1;
			}
			imgCodeKey = DigestUtil.decrypt3Des(imgCodeKey, configManager.getImgCodeDigestBytes());
			String[] cs = imgCodeKey.split("_");
			if(cs == null || cs.length != 3) {
				return -1;
			}
			if(!IMG_CODE_KEY_PREFIX.equals(cs[0])) {
				return -1;
			}
			if(StringUtil.isEmpty(cs[1])) {
				return -1;
			}
		
			long timestamp = Long.parseLong(cs[1]);
			if(System.currentTimeMillis() - timestamp > 300 * 1000) {
				return -2;
			}
			if(imgCode.equalsIgnoreCase(cs[2])) {
				return 0;
			}
		} catch (Exception e) {
			logger.error("error", e);
		}
		return -2;
	}
	
	/*是否线上环境*/
	private Boolean onlineEnv;
	protected boolean isOnlineEnv() {
		if(onlineEnv == null) {
			onlineEnv = Constant.ENV_ONLINE.equals(env);
		}
		return onlineEnv;
	}
}
